﻿using DJH.School.Application.EnumsType;
using DJH.School.Core.Models;
using System;
using System.Linq;

namespace DJH.School.EntityFramework.Data
{
    public class DbInitializer
    {
        public static void Initialize(SchoolDbContext context)
        {
            context.Database.EnsureCreated();
            //检查是否有学生
            if (context.Students.Any())
            {
                return;
            }

            var students = new Student[]
            {
                new Student{RealName="张三丰",EnrollmentDate=DateTime.Parse("1991-01-01")},
                new Student{RealName="李完蛋",EnrollmentDate=DateTime.Parse("1981-03-22")},
                new Student{RealName="和珅",EnrollmentDate=DateTime.Parse("1982-04-11")},
                new Student{RealName="纪晓岚",EnrollmentDate=DateTime.Parse("1990-06-18")},
                new Student{RealName="唐伯虎",EnrollmentDate=DateTime.Parse("1988-05-07")},
                new Student{RealName="李逍遥",EnrollmentDate=DateTime.Parse("1982-12-06")},
                new Student{RealName="乔峰",EnrollmentDate=DateTime.Parse("1993-01-12")},
                new Student{RealName="扫地僧",EnrollmentDate=DateTime.Parse("1995-11-22")},
                new Student{RealName="孙悟空",EnrollmentDate=DateTime.Parse("1997-02-24")}
            };
            foreach (var student in students)
            {
                context.Students.Add(student);
            }

            context.SaveChanges();

            var courses = new Course[]
            {
                new Course { Id = 1001, Title = "语文", Credits = 3 },
                new Course { Id = 1002, Title = "地理", Credits = 3 },
                new Course { Id = 1003, Title = "数学", Credits = 3 },
                new Course { Id = 1004, Title = "英语", Credits = 4 },
                new Course { Id = 1005, Title = "物理", Credits = 4 },
                new Course { Id = 1006, Title = "化学", Credits = 3 },
                new Course { Id = 1007, Title = "生物", Credits = 4 },
            };
            foreach (var course in courses)
            {
                context.Courses.Add(course);
            }

            context.SaveChanges();

            var enrollments = new Enrollment[]
            {
                new Enrollment{StudentId=1,CourseId=1001,Grade=CourseGrade.A},
                new Enrollment{StudentId=1,CourseId=1002,Grade=CourseGrade.B},
                new Enrollment{StudentId=1,CourseId=1003,Grade=CourseGrade.A},
                new Enrollment{StudentId=2,CourseId=1001,Grade=CourseGrade.B},
                new Enrollment{StudentId=2,CourseId=1002,Grade=CourseGrade.A},
                new Enrollment{StudentId=2,CourseId=1003,Grade=CourseGrade.D},
                new Enrollment{StudentId=3,CourseId=1001,Grade=CourseGrade.B},
                new Enrollment{StudentId=3,CourseId=1002,Grade=CourseGrade.A},
                new Enrollment{StudentId=3,CourseId=1003,Grade=CourseGrade.D},
                new Enrollment{StudentId=4,CourseId=1001,Grade=CourseGrade.A},
                new Enrollment{StudentId=4,CourseId=1002,Grade=CourseGrade.D},
                new Enrollment{StudentId=4,CourseId=1003,Grade=CourseGrade.B},
                new Enrollment{StudentId=5,CourseId=1001,Grade=CourseGrade.D},
                new Enrollment{StudentId=5,CourseId=1002,Grade=CourseGrade.A},
                new Enrollment{StudentId=5,CourseId=1003,Grade=CourseGrade.C},
            };

            foreach (var enrollment in enrollments)
            {
                context.Enrollments.Add(enrollment);
            }

            context.SaveChanges();
        }
    }
}